# -*- coding: utf-8 -*-
"""
@Time    : 2023/3/9 17:18
@Author  : JackXia
@Email   : 915624876@qq.com
@QQ      : 915624876
@File    : 3.移除元素.py
@Software: PyCharm
"""
from typing import List


class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        # 快慢指针
        fast = 0  # 快指针
        slow = 0  # 慢指针
        size = len(nums)
        while fast < size:  # 不加等于是因为，a = size 时，nums[a] 会越界
            # slow 用来收集不等于 val 的值，如果 fast 对应值不等于 val，则把它与 slow 替换
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return slow

    def remove_element(self, nums: List[int], val: int) -> int:
        fast = 0
        slow = 0
        size = len(nums)
        while fast < size:
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return slow

    def remove_element1(self, nums: List[int], val: int) -> int:
        count = 0
        for num in nums:
            if num != val:
                count += 1
        return count


if __name__ == '__main__':
    nums = [2, 2, 4,2,2,2,2,4,4,4]
    val = 2
    # print(Solution().remove_element1(nums, val))
    print(Solution().removeElement(nums, val))
